package list.p3_singlelist;

public class MySingleListWithoutHead implements IListWithoutHead{
    private static class Node {
        int val;
        Node next;

        public Node(int val, Node next) {
            this.val = val;
            this.next = next;
        }
    }
    private Node head;

    @Override
    public void addFirst(int data) {
        head = new Node(data, head);
    }

    private Node getLast() {
        if (head == null) return null;
        Node cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        return cur;
    }

    @Override
    public void addLast(int data) {
        Node last = getLast();
        if (last == null) {
            addFirst(data);
        } else {
            last.next = new Node(data, null);
        }
    }

    @Override
    public boolean addIndex(int index, int data) {
        int size = size();
        if (index == 0) addFirst(data);
        if (index > 0 && index < size) {
            Node prev = head;
            while (--index > 0) {
                prev = prev.next;
            }
            prev.next = new Node(data, prev.next);
        }
        if (index == size) addLast(data);
        return false;
    }

    @Override
    public boolean contains(int key) {
        for (Node cur = head; cur != null; cur = cur.next) {
            if (cur.val == key) return true;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (head == null) return;
        Node prev = head;
        if (prev.val == key) {
            head = head.next;
            return;
        }
        for (; prev.next != null; prev = prev.next) {
            if (prev.next.val == key) {
                prev.next = prev.next.next;
                break;
            }
        }
    }

    @Override
    public void removeAllKey(int key) {
        if (head == null) return;
        Node prev = head;
        if (prev.val == key) head = head.next;
        while (prev.next != null) {
            if (prev.next.val == key) {
                prev.next = prev.next.next;
            } else {
                prev = prev.next;
            }
        }
    }

    @Override
    public int size() {
        int size = 0;
        Node cur = head;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    @Override
    public void display() {
        System.out.print("[");
        for (Node cur = head; cur != null; cur = cur.next) {
            System.out.print(cur.next == null ? cur.val : cur.val + " >> ");
        }
        System.out.println("]");
    }

    @Override
    public void clear() {
        head = null;
    }

    public void split(MySingleListWithoutHead head, int x) {
        MySingleListWithoutHead left = new MySingleListWithoutHead();
        MySingleListWithoutHead right = new MySingleListWithoutHead();
        for (Node cur = head.head; cur != null; cur = cur.next) {
            if (cur.val < x) {
                left.addLast(cur.val);
            } else {
                right.addLast(cur.val);
            }
        }
        if (left.head == null) {
            head.head = right.head;
            return;
        }
        if (right.head == null) {
            head.head = left.head;
            return;
        }
        Node last = left.getLast();
        last.next = right.head;
        head.head = left.head;
    }
}
